package com.example.administrator.funchat.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.administrator.funchat.R;
import com.example.administrator.funchat.fragment.Contact_fragment;
import com.example.administrator.funchat.fragment.Friend_fragment;
import com.example.administrator.funchat.fragment.Information_fragment;
import com.example.administrator.funchat.fragment.MySelf_fragment;
import com.example.administrator.funchat.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * 消息,通讯录,朋友圈,个人界面碎片所依赖的活动界面,登录成功后跳转到这里
 * Created by Administrator on 2016/6/23.
 */
public class MainActivity extends FragmentActivity implements View.OnClickListener {

    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments;

    private LinearLayout ll_info;
    private LinearLayout ll_contant;
    private LinearLayout ll_friend;
    private LinearLayout ll_myself;

    private LinearLayout mTabIofo;
    private LinearLayout mTabMail;
    private LinearLayout mTabFrd;
    private LinearLayout mTabMyself;

    private ImageView mImgIofo;
    private ImageView mImgMail;
    private ImageView mImgFrd;
    private ImageView mImgMyself;
    private ImageView mAddIcon;
    public static User u;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        inView();
        initEvent();
        setSelect(1);
        u = getInfo();
        JPushInterface.setAlias(this, u.getPhone(), new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                Log.i("push", "gotResult: ==" + i + "," + s);
            }
        });

    }

    public User getInfo() {
        Log.i("login", "getInfo: " + ((User) getIntent().getSerializableExtra("info")).getAddress());
        return ((User) getIntent().getSerializableExtra("info"));
    }

    //初始化控件
    private void inView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        mTabIofo = (LinearLayout) findViewById(R.id.info_tab);
        mTabMail = (LinearLayout) findViewById(R.id.mail_tab);
        mTabFrd = (LinearLayout) findViewById(R.id.friend_tab);
        mTabMyself = (LinearLayout) findViewById(R.id.myself_tab);

        mImgIofo = (ImageView) findViewById(R.id.info_icon);
        mImgMail = (ImageView) findViewById(R.id.mail_icon);
        mImgFrd = (ImageView) findViewById(R.id.friend_icon);
        mImgMyself = (ImageView) findViewById(R.id.myself_icon);
        mAddIcon = (ImageView) findViewById(R.id.add_icon);

        ll_info = (LinearLayout) findViewById(R.id.ll_info);
        ll_contant = (LinearLayout) findViewById(R.id.ll_contant);
        ll_friend = (LinearLayout) findViewById(R.id.ll_friend);
        ll_myself = (LinearLayout) findViewById(R.id.ll_myself);

        mFragments = new ArrayList<Fragment>();
        Fragment mTab01 = new Information_fragment();
        Fragment mTab02 = new Contact_fragment();
        Fragment mTab03 = new Friend_fragment();
        Fragment mTab04 = new MySelf_fragment();
        mFragments.add(mTab01);
        mFragments.add(mTab02);
        mFragments.add(mTab03);
        mFragments.add(mTab04);

        //实例化ViewPager中的适配器,并加载进mViewPager
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };

        mViewPager.setAdapter(mAdapter);


        //设置ViewPager的监听事件
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int currentItem = mViewPager.getCurrentItem();
                setTab(currentItem);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //根据position加载不同的fragment
    private void setTab(int i) {
        resetImgs();
        switch (i) {
            case 0:
                mImgIofo.setImageResource(R.mipmap.info_icon);
                break;
            case 1:
                mImgMail.setImageResource(R.mipmap.mail_icon);
                break;
            case 2:
                mImgFrd.setImageResource(R.mipmap.friend_icon);
                break;
            case 3:
                mImgMyself.setImageResource(R.mipmap.myself_icon);
                break;
        }
    }

    //初始化事件
    private void initEvent() {
       /* mImgIofo.setOnClickListener(this);
        mImgMail.setOnClickListener(this);
        mImgFrd.setOnClickListener(this);
        mImgMyself.setOnClickListener(this);*/
        mAddIcon.setOnClickListener(this);
        ll_contant.setOnClickListener(this);
        ll_friend.setOnClickListener(this);
        ll_info.setOnClickListener(this);
        ll_myself.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_info:
                setSelect(0);
                break;
            case R.id.ll_contant:
                setSelect(1);
                break;
            case R.id.ll_friend:
                setSelect(2);
                break;
            case R.id.ll_myself:
                setSelect(3);
                break;
            case R.id.add_icon:
                startActivity(new Intent(this, SearchFriendActivity.class));
                break;
        }

    }

    private void setSelect(int i) {
        //设置图片为亮色
        //切换内容区域
        resetImgs();
        switch (i) {
            case 0:
                mImgIofo.setImageResource(R.mipmap.info_icon);
                break;
            case 1:
                mImgMail.setImageResource(R.mipmap.mail_icon);
                break;
            case 2:
                mImgFrd.setImageResource(R.mipmap.friend_icon);
                break;
            case 3:
                mImgMyself.setImageResource(R.mipmap.myself_icon);
                break;
        }

        mViewPager.setCurrentItem(i);
    }

    //切换图片至选中状态
    private void resetImgs() {
        mImgIofo.setImageResource(R.mipmap.info_gray);
        mImgMail.setImageResource(R.mipmap.mail_gray);
        mImgFrd.setImageResource(R.mipmap.friend_gray);
        mImgMyself.setImageResource(R.mipmap.myself_gray);

    }


}
